package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.core.KZD_ApiConstance;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_RechargeCoin;
import com.kzd.sdk.entity.KZD_UserInfo;
import com.kzd.sdk.ui.adapter.KZD_RechargeCoinAdapter;
import com.kzd.sdk.utils.AppUtils;

public class KZD_UserRechargeActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserRechargeActivity.class);
        context.startActivity(intent);
    }

    private TextView tvUserRechargeName, tvUserRechargeCoin, tvUserRechargeMoney;
    private EditText etUserRechargeMoney;
    private RadioButton rbUserRechargeWx, rbUserRechargeAli;

    private int mPayType = 0;
    private String mMoney, mCoinId;

    private KZD_RechargeCoinAdapter mRechargeCoinAdapter;

    private KZD_HttpCallback<KZD_RechargeCoin> callbackRechargeCoin = new KZD_HttpCallback<KZD_RechargeCoin>() {
        @Override
        public void onSuccess(KZD_RechargeCoin data) {
            if (data.getData().isEmpty()) {
                return;
            }
            mRechargeCoinAdapter.setNewInstance(data.getData());
        }
    };

    private KZD_HttpCallback<String> callbackRechargeOrder = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            String url = KZD_ApiConstance.HTTP_ORDER_PAY +
                    "&id=" + data;
            KZD_WebRichActivity.openUrl("支付",url);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_recharge";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {

        findViewById(AppUtils.getId((this),"ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
        tvUserRechargeName = findViewById(AppUtils.getId((this), "tvUserRechargeName"));
        tvUserRechargeCoin = findViewById(AppUtils.getId((this), "tvUserRechargeCoin"));
        tvUserRechargeMoney = findViewById(AppUtils.getId((this), "tvUserRechargeMoney"));
        etUserRechargeMoney = findViewById(AppUtils.getId((this), "etUserRechargeMoney"));
        rbUserRechargeWx = findViewById(AppUtils.getId((this), "rbUserRechargeWx"));
        rbUserRechargeAli = findViewById(AppUtils.getId((this), "rbUserRechargeAli"));

        mRechargeCoinAdapter = new KZD_RechargeCoinAdapter((this));

        RecyclerView rvUserRechargeBody = findViewById(AppUtils.getId((this), "rvUserRechargeBody"));
        rvUserRechargeBody.setLayoutManager(new GridLayoutManager((this), (4)));
        rvUserRechargeBody.setAdapter(mRechargeCoinAdapter);

        mRechargeCoinAdapter.setOnItemClickListener(new KZD_OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                KZD_RechargeCoin.CoinInfo itemData = mRechargeCoinAdapter.get(position);
                StringBuilder money = new StringBuilder().append(itemData.getMoney())
                        .append("CNY=").append(itemData.getCoin()).append("平台币");
                tvUserRechargeMoney.setText(money);
                etUserRechargeMoney.setText(null);
                mCoinId = itemData.getId();
                mMoney = null;
            }
        });

        etUserRechargeMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    mRechargeCoinAdapter.setSelectPosition(-1);
                    tvUserRechargeMoney.setText(null);
                    mCoinId = null;
                }
                mMoney = s.toString();
            }
        });

        rbUserRechargeWx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    return;
                }
                mPayType = 2;
            }
        });

        rbUserRechargeAli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    return;
                }
                mPayType = 1;
            }
        });


        findViewById(AppUtils.getId((this), "tvUserRechargeSubmit"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(mCoinId) && TextUtils.isEmpty(mMoney)) {
                            toast("请选择或输入充值面额");
                            return;
                        }
                        if (mPayType == 0) {
                            toast("请选择支付类型");
                            return;
                        }
                        KZD_ApiManager.httpRechargeOrder(mCoinId, mMoney, String.valueOf(mPayType), callbackRechargeOrder);
                    }
                });
    }

    @Override
    public void initData() {

        KZD_UserInfo userInfo = KZD_SdkController.getUserInfo();
        if (userInfo != null) {
            tvUserRechargeName.setText(new StringBuilder("用户名：").append(userInfo.getUser_name()));
            tvUserRechargeCoin.setText(new StringBuilder("平台币：").append(userInfo.getCoin()));
        }
        KZD_ApiManager.httpRechargeCoin(callbackRechargeCoin);

    }
}
